<?php
/**
 * Chat
 */
class controller_chat extends controller_base {
	/**
	 * index
	 */
	public function action_index() {
		echo time();
	}
	
	/**
	 * 表单
	 */
	public function action_form() {
		if(empty($_GET['from']) || empty($_GET['to']) || $_GET['to'] == $_GET['from']) {
			throw new common_exception('发送人、接收人异常');
		}
		
		$from = $_GET['from'];
		$to = $_GET['to'];
		
		$this->assign('from', $from);
		$this->assign('to', $to);
	}
	
	/**
	 * 列表iframe页
	 */
	public function action_list() {}
	
	/**
	 * 获取聊表记录
	 */
	public function action_list_data() {
		set_time_limit(0);
		
		$model_chatlist = new model_chatlist();
		$last_lid = $_GET['last_lid'];
		while(count($model_chatlist->find_all("lid>$last_lid", '', '1', 'lid')) == 0) {
			// 休息
			usleep(0.1*1000000);
		}
		
		$res = $model_chatlist->find_all(1, 'created DESC', 20);
			
		foreach ($res as &$row) {
			$row['created_time'] = date('H:i:s', $row['created']);
		}
		echo $_GET['jsoncallback'].'('.json_encode($res).')';
	}
	
	/**
	 * 发送消息 (POST:from, to, content)
	 */
	public function action_save() {
		$data = $_POST;
		$data['created'] = time();
		
		$model_chatlist = new model_chatlist();
		$model_chatlist->create($data);
	}
}
?>